package com.interagriculture.service.impl;

import com.github.pagehelper.PageHelper;
import com.interagriculture.common.Constants;
import com.interagriculture.common.CustomException;
import com.interagriculture.common.PageResult;
import com.interagriculture.entity.TeacherInfo;
import com.interagriculture.entity.query.TeacherInfoQuery;
import com.interagriculture.enums.ExceptionCodeEnum;
import com.interagriculture.mapper.TeacherInfoMapper;
import com.interagriculture.service.TeacherInfoService;
import com.interagriculture.utils.UploadFileUtil;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TeacherInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-11-13 22:08:27
 */
@Service("teacherInfoService")
public class TeacherInfoServiceImpl implements TeacherInfoService {
    @Resource
    private TeacherInfoMapper teacherInfoMapper;
    @Resource
    private UploadFileUtil uploadFileUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param teacherId 主键
     * @return 实例对象
     */
    @Override
    public TeacherInfo queryById(Integer teacherId) {
        return this.teacherInfoMapper.queryById(teacherId);
    }

    /**
     * 新增数据
     *
     * @param teacherInfo 实例对象
     * @return 实例对象
     */
    @Override
    public TeacherInfo insert(TeacherInfo teacherInfo) {
        this.teacherInfoMapper.insert(teacherInfo);
        return teacherInfo;
    }

    /**
     * 修改数据
     *
     * @param teacherInfo 实例对象
     * @return 实例对象
     */
    @Override
    public TeacherInfo update(TeacherInfo teacherInfo) {
        this.teacherInfoMapper.update(teacherInfo);
        return this.queryById(teacherInfo.getTeacherId());
    }

    /**
     * 通过主键删除数据
     *
     * @param teacherId 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Integer teacherId) {
        teacherInfoMapper.deleteById(teacherId);
    }

    /**
     * 保存或更新
     * @param teacherInfo
     */
    @Override
    public void saveOrUpdateInfo(TeacherInfo teacherInfo) {
        if(teacherInfo.getTeacherId()==null){
            teacherInfoMapper.insert(teacherInfo);
        }else {
            teacherInfoMapper.update(teacherInfo);
        }

    }

    /**
     * 分页查询
     * @param teacherInfoQuery
     * @return
     */
    @Override
    public PageResult findByPage(TeacherInfoQuery teacherInfoQuery) {
        if(teacherInfoQuery!=null && teacherInfoQuery.getPageNum()!=null && teacherInfoQuery.getPageSize()!=null){
            PageHelper.startPage(teacherInfoQuery.getPageNum(),teacherInfoQuery.getPageSize());
        }
        if(teacherInfoQuery!=null && teacherInfoQuery.getOrderBy()!=null){
            PageHelper.startPage(teacherInfoQuery.getOrderBy());
        }
        List<TeacherInfo> teacherInfos = teacherInfoMapper.queryAllByPage(teacherInfoQuery);
        long count = teacherInfoMapper.count(teacherInfoQuery);
        return new PageResult(count,teacherInfos);
    }
}
